package com.icss.th.db.tools;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.icss.th.common.config.SystemConfig;
import com.icss.th.common.util.DBUtil;

public class LuceneIndex {

	private Directory dir;
	private IndexWriter writer;
	
	
	/*
	 * 初始化试题索引
	 */
	public void createIndexExam(){
		Connection conn = null;
		PreparedStatement stmt = null; 
		ResultSet rs = null;
		
		//分批建立索引
		Long fallyNum = new Long(2100000); 
		Long curNum = new Long(1000);
		Long lastNum = new Long(0);
		
		try {
			conn = DBUtil.getConn();
			dir = FSDirectory.open(new File(SystemConfig.EXAM_INDEX_PATH));
			writer = initWriter(); 
			
			for(; curNum < fallyNum; curNum = curNum + 1000, lastNum = lastNum + 1000){
				System.out.println("====当前正在建立试题索引第"+lastNum+"至"+curNum+"条数据=======");
				String sql =" SELECT * FROM EXAMDATA WHERE ID <= '" + curNum + "' AND ID > '" + lastNum + "' ";
				stmt = conn.prepareStatement(sql); 
				rs = stmt.executeQuery();
				while(rs.next()){
					Document doc=new Document();
					doc.add(new StringField("EXAMID", rs.getString("EXAMID") != null ? rs.getString("EXAMID") : "" , Field.Store.YES));  
					doc.add(new TextField("EXAMTITLE", rs.getString("SHORTEXAMTITLE") != null ? rs.getString("SHORTEXAMTITLE") : "", Store.YES));  
					doc.add(new StringField("KEYTYPE", rs.getString("KEYTYPE") != null ? rs.getString("KEYTYPE") : "", Field.Store.YES));  
					doc.add(new IntField("REFERYEAR", rs.getInt("REFERYEAR") > 0 ? rs.getInt("REFERYEAR") : 0, Field.Store.YES));  
					doc.add(new StringField("INPAPERID", rs.getString("INPAPERID") != null ? rs.getString("INPAPERID") : "", Field.Store.YES));  
					doc.add(new StringField("PAPERTITLE", rs.getString("PAPERTITLE") != null ? rs.getString("PAPERTITLE") : "", Field.Store.YES));  
					doc.add(new StringField("EXAMANSWER", rs.getString("EXAMANSWER") != null ? rs.getString("EXAMANSWER") : "", Field.Store.YES));  
					writer.addDocument(doc);  
				}
				rs.close();
				stmt.close();
			}
			System.out.println("====试题索引创建完成====");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(conn != null){
					conn.close();
				}
				if(writer != null){
					writer.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/*
	 * 初始化试卷索引
	 */
	public void createIndexPaper(){
		Connection conn = null;
	    PreparedStatement stmt = null; 
	    ResultSet rs = null;
	    
	    //分批建立索引
	    Long fallyNum = new Long(2100000); 
	    Long curNum = new Long(1000);
		Long lastNum = new Long(0);
		
		try {
			conn = DBUtil.getConn();
			dir = FSDirectory.open(new File(SystemConfig.PAPER_INDEX_PATH));
			writer = initWriter(); 
			
			for(; curNum < fallyNum; curNum = curNum + 1000, lastNum = lastNum + 1000){
				System.out.println("====当前正在建立试卷索引第"+lastNum+"至"+curNum+"条数据=======");
				String sql =" SELECT * FROM PAPERINFO WHERE ID <= '" + curNum + "' AND ID > '" + lastNum + "' ";
		        stmt = conn.prepareStatement(sql); 
		        rs = stmt.executeQuery();
		        while(rs.next()){
		        	Document doc=new Document();
		        	doc.add(new StringField("PAPERID", rs.getString("PAPERID") != null ? rs.getString("PAPERID") : "" , Field.Store.YES));  
		        	doc.add(new TextField("PAPERTITLE", rs.getString("PAPERTITLE") != null ? rs.getString("PAPERTITLE") : "", Store.YES));  
		        	doc.add(new IntField("REFERYEAR", rs.getInt("REFERYEAR") > 0 ? rs.getInt("REFERYEAR") : 0, Field.Store.YES));    
		        	doc.add(new StringField("KEYTYPE", rs.getString("KEYTYPE") != null ? rs.getString("KEYTYPE") : "", Field.Store.YES));  
		        	doc.add(new StringField("TYPENAME", rs.getString("TYPENAME") != null ? rs.getString("TYPENAME") : "", Field.Store.YES));  
		        	writer.addDocument(doc);  
		        }
		        rs.close();
		        stmt.close();
			}
			System.out.println("====试卷索引创建完成====");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(conn != null){
					conn.close();
				}
				if(writer != null){
					writer.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/** 
     * 获得IndexWriter对象 
     * @return 
     * @throws Exception 
     */  
    public IndexWriter initWriter() throws Exception {  
        Analyzer analyzer = new IKAnalyzer(true);  
        IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer);  
        return new IndexWriter(dir, iwc);  
    }  
	

}
